// 引入公共组件
require('../less/commCss/basic.css')
// 引入当前css
require('../less/training-course.less')

const {
    $http
} = require('../lib/commonJs/http.js')
const utils = require('../lib/commonJs/utils.js')

// 是否登录
utils.isLogin()

// 导航栏
utils.showNav('sport')

// 小标题
utils.sprotNav('training-course')

/**
 * 获取元素
 */
const appTrainingCourse = document.querySelector('.app-training-course')
const newCourse = document.querySelector('.new-course')
const course = document.querySelector('.course')




// 渲染课程
function showCourse(){
    $http.get(`/sports/courseList/?id=${localStorage.getItem('userId')}`, function ({
        data
    }) {
        let coursenewHtml = ''
        let coursenormalHtml = ''
    
        // 新课程
        const coursenew = data.filter((item) => {
            return item.latest === 1
        })
        // console.log(data);
        coursenew.forEach((item) => {
            coursenewHtml += `
            <div class="course-box mt8" data-courseId="${item.courseId}"  style="background-image: url('http://139.9.177.51:8099${item.imgurl}')">
                <div class="course-bottom-body">
                    <p class="course-body-title f16">${item.name}</p>
                    <p class="course-body-content f12 fwbold colro-9A9A9A mt8">${item.desc}</p>
                </div>
            </div>
            `
        })
        newCourse.innerHTML += coursenewHtml
    
        // // 基本课程
        const coursenormal = data.filter((item) => {
            return item.latest === 0
        })
    
        coursenormal.forEach((item) => {
            coursenormalHtml += `
            <div class="course-box mt16" data-courseId="${item.courseId}" style="background-image: url('http://139.9.177.51:8099${item.imgurl}')">
                <div class="course-innerbox-body">
                    <p class="course-body-title f20">${item.name}</p>
                    <p class="course-body-content f14 mt8">${item.desc}</p>
                </div>
            </div>
            `
        })
        course.innerHTML += coursenormalHtml
    })
}
showCourse()

// 点击跳转
appTrainingCourse.addEventListener('click',function(e){
    if(e.target.className.includes('course-box') || e.target.parentElement.className.includes('course-box') || e.target.parentElement.parentElement.className.includes('course-box')){
        let id = e.target.dataset.courseid || e.target.parentElement.dataset.courseid || e.target.parentElement.parentElement.dataset.courseid
        localStorage.setItem('courseId',id)
        location.href = `./course-particulars.html?id=${id}`
    }
})